package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.ExhibitionApplyAdapter;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---参观申请
 */
public class ExhibitionApplyActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private TextView tv_right;
    private ListView my_listView;

    private ExhibitionApplyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exhibition_apply);

        initData();
        initView();
    }

    @Override
    protected void onResume() {
        upLoad();
        super.onResume();
    }

    private List<ExhibitonBean.InfoEntity> info1 = new ArrayList<>();

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        showDialog();
        XUtil.Post(Url.INFORMATION_INFORMATION, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        Log.e("----", "onSuccess:= " + info);
//                        JSONArray json = new JSONArray("info");
//                        for (int i = 0; i < json.length(); i++) {
                        ExhibitonBean bean = gson.fromJson(result, ExhibitonBean.class);
                        info1 = bean.getInfo();

//                        }
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                setData();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    private void setData() {
        mAdapter = new ExhibitionApplyAdapter(info1, this, this);
        my_listView.setAdapter(mAdapter);
    }

    private void initData() {

    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("参观申请");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.submit).setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setOnClickListener(this);
        tv_right = (TextView) findViewById(R.id.submit);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("申请");
        tv_right.setTextColor(0xff008c99);

        my_listView = (ListView) findViewById(R.id.my_listView);

    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //申请
            case R.id.submit:
                intent = new Intent(this, ApplyMessageActivity.class);
                startActivity(intent);
                break;

            //修改申请
            case R.id.tv_change:
                int position2 = (int) v.getTag();
                intent = new Intent(this, ApplyMessageActivity.class);
                intent.putExtra("position2", info1.get(position2).getList_id());

                startActivity(intent);
                break;

            //取消申请
            case R.id.tv_cancel:
                int position = (int) v.getTag();
                cancleInfo(position);
                //MyToast("取消申请");
                break;
            case R.id.look:
                int position3 = (int) v.getTag();
                intent = new Intent(this, ApplyMessageActivity.class);
                intent.putExtra("position2", info1.get(position3).getList_id());
                intent.putExtra("look", "1");
                startActivity(intent);

                break;


            default:
                break;
        }
    }

    private void cancleInfo(int posi) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("info_id", info1.get(posi).getList_id());
        showDialog();
        XUtil.Post(Url.INFORMATION_CANCELINFO, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        upLoad();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }
}
